<?php

namespace app\common\model;

use think\Db;
use think\Exception;
use think\Model;

class DepositRefund extends Model
{
    // 表名
    protected $name = 'deposit_refund';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'status_text'
    ];
    
	const STATUS_W = 0;
	const STATUS_Y = 1;
	const STATUS_N = 2;
    
	public static $statusList = [
		self::STATUS_Y => '已退款',
		self::STATUS_W => '审核中',
		self::STATUS_N => '申请不通过'
	];

	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['status'];
		$list = self::$statusList;
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

	public function user(){
		return $this->belongsTo('user', 'user_id', 'id','', 'left')->setEagerlyType(0);
	}
	
	public function order(){
		return $this->belongsTo('order', 'order_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function orderGoods(){
		return $this->belongsTo('orderGoods', 'order_goods_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function shipping(){
		return $this->belongsTo('shipping', 'shipping_id', 'id', '', 'left')->setEagerlyType(0);
	}

	
	public function fund($row) {
		$this->startTrans();
		try {
			
			//更改记录状态
			$res = $this->save([
				'status' => self::STATUS_Y,
			], ['id' => $row['id']]);
			
			if(!$res) {
				throw new Exception('更改状态出错');
			}
			
			//关联的订单数据
			$order = Db::name('order')->where(['id' => $row['order_id']])->find();
			
			//用户数据
			$user = Db::name('user')->find($row['user_id']);
			
			//减押金
			$fundModel = new FundsRecord();
			
			$depositRes = $fundModel->addRecord($row['user_id'], $order['order_amount'],$user['deposit_balance'], $fundModel::ACCOUNT_TYPE_DEPOSIT, $fundModel::OPE_TYPE_LESS, $fundModel::TYPE_FUND_DEPOSIT, $row['order_id'], '退还商品押金到余额');
			if(!$depositRes) {
				throw new Exception('记录押金流水出错');
			}
			
			$balRes = $fundModel->addRecord($row['user_id'], $order['order_amount'], $user['balance'], $fundModel::ACCOUNT_TYPE_BALANCE, $fundModel::OPE_TYPE_PLUS, $fundModel::TYPE_FUND_DEPOSIT, $row['order_id'], '退还商品押金到余额');
			if(!$balRes) {
				throw new Exception('记录余额流水出错');
			}
			
			//加余额
			$userModel = new User();
			$userRes = $userModel->save([
				'balance' => $user['balance'] + $order['order_amount'],
				'deposit_balance' => $user['deposit_balance'] - $order['order_amount']
			], ['id' => $row['user_id']]);
			
			if(!$userRes) {
				throw new Exception('操作资金出错');
			}
			
			$this->commit();
			return true;
		} catch (Exception $e) {
			$this->rollback();
			$this->error = $e->getMessage();
			return false;
		}
	}

}
